package com.mtx.weatherreport.Application;

import android.app.Application;
import android.os.Environment;

import com.mtx.weatherreport.bean.City;
import com.mtx.weatherreport.bean.CityDB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinghuayu
 * @create 2018/2/26.
 */

public class MyApplication extends Application {
    private static Application mApplication;
    private List<City> mCityList;
    private CityDB mCityDB;

    private ArrayList<String> mCityName = new ArrayList<String>();  // 保存城市名称
    private ArrayList<String> mCityNumber = new ArrayList<>();   // 保存城市编号
    private ArrayList<String> mCityName2 = new ArrayList<>();  //保存城市名称
    private ArrayList<String> mCityNumber2 = new ArrayList<>();  // 保存城市编号


    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;

        mCityDB = openCityDB();  // 初始化数据库
        initCityList();  // 初始化城市信息列表
    }

    // 初始化数据库
    private CityDB openCityDB() {
        // 这个路径是在sdcard下边的，有的手机没有sd卡，所以配置了一个虚拟的sdcard路径
        String path = "/data" + Environment.getDataDirectory().getAbsolutePath()
                + File.separator + getPackageName()
                + File.separator + "databases"  //File.separator是与系统有关的系统分隔符
                + File.separator;  // 文件夹路径

        File dbFile = new File(path);  // 目录参数
        if(!dbFile.isDirectory()){
            dbFile.mkdir();  // 创建文件夹
        }
        // 创建的只是一种联系，既不是文件，也不是路径
        File db = new File(dbFile + File.separator + CityDB.CITY_DB_NAME);
        File ex_file = new File(dbFile, CityDB.CITY_DB_NAME);
        try {
            ex_file.createNewFile();  // 创建文件，不然后边会出错
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            InputStream is = getAssets().open("city.db");
            FileOutputStream fos = new FileOutputStream(db);
            int len = -1;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                fos.flush();
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
        return new CityDB(this, db);
    }

    // 初始化城市信息列表
    private void initCityList() {
        mCityList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                prepareCityList();
            }
        }).start();
    }

    private boolean prepareCityList() {
        mCityList = mCityDB.getAllCity();
        for(City city:mCityList) {
            String cityName = city.getmCity();
            String provinceName = city.getmProvince();
            String cityNumber = city.getmNumber();
            mCityName.add(cityName+"      "+provinceName);
            mCityName2.add(cityName+"      "+provinceName);
            mCityNumber.add(cityNumber);
            mCityNumber2.add(cityNumber);
        }
        return true;
    }

    //获取城市和省份名称列表
    public ArrayList<String> getListCity(){
        return mCityName;
    }
    public ArrayList<String> getListCity2() {return mCityName2; }

    //只获取城市编号
    public ArrayList<String> getmCityNumber(){
        return mCityNumber;
    }
    public ArrayList<String> getmCityNumber2(){
        return mCityNumber2;
    }

}

